<template>
  <section>
    <div class="section-header">合同基本信息</div>
    <a-form-model ref="ruleForm">
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="合同编号：">
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.contractNumber"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item ref="projectRealName" label="合同名称：">
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.contractName"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="合同金额：">
            <!-- <a-input v-model="project.hatchProjectName" />item> -->
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.amount"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="采购订单号：">
            <a-input disabled v-model="project.busKey" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="签订公司/代码：">
            <a-select
              :value="project.signCompany + '/' + project.signCompanyCode"
              @change="(v) => changeSignCompanyCode(v)"
            >
              <a-select-option
                v-for="(item, index) in signCompany"
                :key="'companyName' + index"
                :value="item.componyNo"
                :disabled="!checkPermission(['WORK_REPORT_MANAGER'])"
              >
                {{ item.componyName + '/' + item.componyNo }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item ref="projectRealName" label="供应商名称/编码：">
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              :value="
                project.supplierName && project.supplierCode ? project.supplierName + '/' + project.supplierCode : ''
              "
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="费用支出号：">
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.costNumber"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="SPA项目号：">
            <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.sapCode"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="合同签订主管">
            <UserInputSelector
              :username.sync="project.deptHeadId"
              :realname.sync="project.deptHead"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="default"
              placeholder="请选择"
            />
            <!-- <a-input
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              v-model="project.projectCode"
            /> -->
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="项目负责人：">
            <UserInputSelector
              :username.sync="project.followPersonId"
              :realname.sync="project.followPerson"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="default"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="合同签订人：">
            <UserInputSelector
              :username.sync="project.signUserId"
              :realname.sync="project.signUser"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="default"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="合同跟进专员：">
            <UserInputSelector
              :username.sync="project.createOrderUserId"
              :realname.sync="project.createOrderUser"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="default"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="项目经理：">
            <UserInputSelector
              :username.sync="project.proManagerId"
              :realname.sync="project.proManager"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="default"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="水电费承担：">
            <div class="disabled-radio">
              <a-radio-group
                name="radioGroup"
                v-model="project.waterAndElectricityPay"
                :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              >
                <a-radio v-for="(code, index) in codes.partA_B" :key="index" :value="code.code">
                  {{ code.name }}
                </a-radio>
              </a-radio-group>
            </div>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="卸货费承担：">
            <div class="disabled-radio">
              <a-radio-group
                name="radioGroup"
                v-model="project.unloadAmount"
                :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              >
                <a-radio v-for="(code, index) in codes.partA_B" :key="index" :value="code.code">
                  {{ code.name }}
                </a-radio>
              </a-radio-group>
            </div>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item label="报工人">
            <UserInputSelector
              :username.sync="project.reportUserId"
              :realname.sync="project.reportUser"
              :disabled="true"
              multiple="multiple"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item label="参与人">
            <UserInputSelector
              :username.sync="project.joinUserId"
              :realname.sync="project.joinUser"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
              multiple="multiple"
              placeholder="请选择"
            />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="是否数字贷：">
            <a-radio-group
              name="radioGroup"
              v-model="project.digitalLoan"
              @change="changeDigitalLoan"
              :disabled="!checkPermission(['WORK_REPORT_ADMIN', 'WORK_REPORT_MANAGER'])"
            >
              <a-radio v-for="(code, index) in yesOrNot" :key="index" :value="code.code">
                {{ code.name }}
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="16">
          <a-form-model-item label="备案证名称：">
            <a-select v-model="project.filingName" :disabled="!project.digitalLoan">
              <a-select-option
                v-for="(item, index) in codes['数字贷备案证名称']"
                :key="'filingName' + index"
                :value="item.name"
              >
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </section>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
export default {
  props: {
    project: {
      type: Object,
      default: () => {
        return {}
      },
    },
    permission: {
      type: Array,
      default: () => {
        return []
      },
    },
    signCompany: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  data() {
    return {
      urls: {
        project: '/workReport/project/info',
        psCodes: '/hte/contract/codes',
        personList: '/person/info/personList',
        codes: '/manage/codeInfo/queryByTypes',
      },
      codes: {},
      personList: [],
      personMap: {},
      existPersonMap: {},
      selectUserKeyWord: null,
      selectUserTimeOut: null,
      yesOrNot: [
        { code: false, name: '否' },
        { code: true, name: '是' },
      ],
    }
  },
  mounted() {
    // this.fetchProject()
    this.fetchPsCodes()
    this.fetchCodes()
  },
  methods: {
    fetchCodes() {
      let types = []
      types.push('数字贷备案证名称')
      getAction(this.urls.codes, { types: types.join(',') }).then((res) => {
        if (res.success) {
          this.codes = { ...this.codes, ...res.result }
        }
      })
    },
    checkPermission(powers) {
      let hasPower = false
      for (let p of powers) {
        if (this.permission.indexOf(p) > -1) {
          hasPower = true
          break
        }
      }
      return hasPower
    },
    fetchPsCodes() {
      postAction(this.urls.psCodes, { codes: ['partA_B'] }).then((res) => {
        if (res.success) {
          this.codes = { ...this.codes, ...res.result }
        }
      })
    },
    clickPersonSelector(type, mutil) {
      this.currentType = type
      this.mutil = mutil
      let usernames = this.custom[type + 'Id'] ? this.custom[type + 'Id'].split(',') : []
      let realnames = this.custom[type] ? this.custom[type].split(',') : []
      for (let i = 0; i < realnames.length; i++) {
        this.$set(this.existPersonMap, realnames[i], usernames[i])
      }
    },
    changeSignCompanyCode(v) {
      this.project.signCompanyCode = v
      if (this.project.signCompanyCode) {
        this.signCompany.forEach((s) => {
          if (s.componyNo == this.project.signCompanyCode) {
            this.project.signCompany = s.componyName
          }
        })
      } else {
        this.project.signCompany = ''
      }
    },
    changeDigitalLoan() {
      if (!this.project.digitalLoan) {
        this.$set(this.project, 'filingName', '')
      }
    },
  },
  watch: {
    // project:{
    // handler(newV, oldV) {
    //   if(this.project.projectId){
    //     this.fetchPermission()
    //   }
    // },
    // deep:true
    // }
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
</style>
